package org.itboys.weixin.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.io.FileUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.weixin.entity.WeixinConfig;
import org.itboys.weixin.entity.WeixinMessage;
import org.itboys.weixin.entity.WeixinMessageDetail;
import org.itboys.weixin.service.WeixinConfigService;
import org.itboys.weixin.service.WeixinMessageDetailService;
import org.itboys.weixin.service.WeixinMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;

@RestController
@RequestMapping(value = "/weixin/messagedetail")
public class WeixinMessageDetailController extends BaseController {

	@Autowired
	private WeixinMessageDetailService weixinMessageDetailService;
	@Autowired
	private WeixinMessageService weixinMessageService;
	@Autowired
	private WeixinConfigService weixinConfigService;
	@Autowired
	private ResourceHolder resourceHolder;

	@RequestMapping("/vm")
	public String projectMenus(HttpServletRequest request, Model model) {
		return "/weixin/messagedetail/list";
	}

	@RequestMapping("/list")
	public void list(HttpServletResponse response, HttpServletRequest request) {
		Map<String, Object> sqlMap = QueryParamUtils.builderQueryMap(request);
		Page<WeixinMessageDetail> page = weixinMessageDetailService.pageQuery2(
				request, sqlMap);
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

	@RequestMapping("/getById")
	public ModelAndView getById(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Long wxMsgId = Long.valueOf(request.getParameter("wxMsgId"));
		WeixinMessage wx = weixinMessageService.getById(Long.valueOf(wxMsgId));
		model.addAttribute("wx", wx);
		model.addAttribute("wxMsgId", wxMsgId);
		model.addAttribute("weixinId", wx.getWeixinId());
		if (wx.getMsgType() != 3) {
			Map<String, Object> sqlMap = Maps.newHashMap();
			sqlMap.put("wxMsgId", wxMsgId);
			if (weixinMessageDetailService.list(sqlMap).size() == 1) {
				WeixinMessageDetail info = weixinMessageDetailService.list(
						sqlMap).get(0);
				model.addAttribute("info", info);
			}
			/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
			model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
			model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/
			return new ModelAndView("/weixin/messagedetail/input");
		} else {
			String id = request.getParameter("id");
			if (StringUtils.isNotBlank(id)) {
				WeixinMessageDetail info = weixinMessageDetailService
						.getById(Long.valueOf(id));
				model.addAttribute("info", info);
			}
			String flag = request.getParameter("flag");
			if (StringUtils.isNotBlank(flag)) {
				/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
				model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
				model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/
				return new ModelAndView("/weixin/messagedetail/input");
			} else {
				return new ModelAndView("/weixin/messagedetail/list");
			}
		}
	}

	@RequestMapping("/save1")
	public void insert1(HttpServletResponse response,
			@ModelAttribute WeixinMessageDetail weixinMessageDetail) {
		try {
			if (weixinMessageDetail.getId() == 0L) {
				weixinMessageDetailService.save(weixinMessageDetail);
			} else {
				weixinMessageDetailService.update(weixinMessageDetail);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("weixinmessagedetail insert1 error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	@RequestMapping("/save")
	public void insert(HttpServletResponse response,
			@ModelAttribute WeixinMessageDetail weixinMessageDetail,
			@RequestParam("imgFile") MultipartFile imgFile) {
		try {
			if (imgFile != null && !imgFile.isEmpty()) {
				String filePath = FileUtils.saveFile(imgFile.getInputStream(),
						resourceHolder.getStringValue("fileUploadPath"),
						imgFile.getOriginalFilename(), imgFile.getContentType());
				weixinMessageDetail.setImg(filePath);
			}
			if (weixinMessageDetail.getId() == 0L) {
				weixinMessageDetailService.save(weixinMessageDetail);
			} else {
				weixinMessageDetailService.update(weixinMessageDetail);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("weixinmessagedetail insert error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, HttpServletRequest request) {
		try {
			Long id = Long.valueOf(request.getParameter("id"));
			AjaxUtils.renderText(response,
					String.valueOf(weixinMessageDetailService.delete(id)));
		} catch (Exception e) {
			logger.error("weixinmessagedetail delete error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}

	@RequestMapping("/detail")
	public ModelAndView detail(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String weixinId = request.getParameter("weixinId");
		WeixinConfig weixinConfig = weixinConfigService.getById(Long
				.valueOf(weixinId));
		model.addAttribute("wxName", weixinConfig.getName());
		String id = request.getParameter("id");
		WeixinMessageDetail info = weixinMessageDetailService
				.getById(Long.valueOf(id));
		model.addAttribute("info", info);
		/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
		model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
		model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/
		return new ModelAndView("/weixin/messagedetail/detail");
	}
}
